﻿package com.ufenlei.LTS{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.display.FrameLabel;

	public class Input extends MovieClip {
		protected var value:Boolean = false;
		//关键帧名字
		public static const FRAME_FOCUS:String = "ff";
		public static const FRAME_BLUR:String = "fb";
		public static const FRAME_SELECTED_FOCUS:String = "fsf";
		public static const FRAME_SELECTED_BLUR:String = "fsb";
		public static const FRAME_LOCKED:String = "fl";
		// input会发送的事件
		public static const CLICK:String = "Input_CLICK";
		public static const MOUSE_OVER:String = "Input_MOUSEOVER";
		public static const MOUSE_DOWN:String = "Input_MOUSEDOWN";
		public static const MOUSE_UP:String = "Input_MOUSEUP";
		public static const MOUSE_OUT:String = "Input_MOUSEOUT";
		public static const SELECTED:String = "Input_INPUTSELECTED";
		public static const UNSELECTED:String = "Input_UNINPUTSELECTED";
		public static const CHANGE:String ="INPUT_CHANGE";
		public var enableListening:Boolean;
		
		private var listenerList:Array = new Array();
		private var focusClips:Array = new Array();
		private var blurClips:Array = new Array();
		private var lockedClips:Array = new Array();
		private var selectedFocusClips:Array = new Array();
		private var selectedBlurClips:Array = new Array();
		
		public function Input(defaultValue:Boolean = false,locked:Boolean = false){
			this.value = defaultValue;
			this.enableListening = !locked;
		}
		public function getValue():Boolean{
			return this.value;
		}

		public function select():void {
			this.value = true;
			var selectedEvent:Event = new Event("Input_INPUTSELECTED");
			this.dispatchEvent(selectedEvent);
			initView();
		}
		protected function addViewClip(clip:MovieClip):void{
			for each (var lb:FrameLabel in clip.currentLabels){
				switch( lb.name ){
					case "ff":
						focusClips.push(clip);
						break;
					case "fb":
						blurClips.push(clip);
						break;
					case "fsf":
						selectedFocusClips.push(clip);
						break;
					case "fsb":
						selectedBlurClips.push(clip);
						break;
					case "fl":
						lockedClips.push(clip);
						break;
				}
			}
		}
		protected function initView():void{
			if(!this.enableListening){
				showLocked();
				return;
			}
			if(this.value){
				mouseHit()?showSelectedFocus():showSelectedBlur();
			}else{
				mouseHit()?showFocus():showBlur();
			};
		}
		public function unselect():void{
			this.value = false;
			var unselectedEvent:Event = new Event("Input_UNINPUTSELECTED");
			this.dispatchEvent(unselectedEvent);
			this.initView();
		}
		//mousehit() 判断鼠标有没有在侦测元件上
		private function mouseHit():Boolean{
			var gp:Point = this.localToGlobal(new Point(this.mouseX,this.mouseY));			
			for each(var s:Sprite in this.listenerList){
				if( s.hitTestPoint(gp.x,gp.y, true) ){
					return true;
				}
			}
			return false;
		}
		protected function mouseOver(evt:MouseEvent):void{
			value?showSelectedFocus():showFocus();
			var mouseOverEvent:Event = new Event("Input_MOUSEOVER");
			this.dispatchEvent(mouseOverEvent);
		}
		protected function mouseOut(evt:MouseEvent):void{
			value?showSelectedBlur():showBlur();
			var mouseOutEvent:Event = new Event("Input_MOUSEOUT");
			this.dispatchEvent(mouseOutEvent);
		}
		protected function mouseUp(evt:MouseEvent):void{
			var mouseUpEvent:Event = new Event("Input_MOUSEUP");
			this.dispatchEvent(mouseUpEvent);
		}
		protected function mouseDown(evt:MouseEvent):void{
			var mouseDownEvent:Event = new Event("Input_MOUSEDOWN");
			this.dispatchEvent(mouseDownEvent);
		}
		protected function mouseClick(evt:MouseEvent):void{
			onClick();
			var e:Event = new Event(CLICK,true);
			this.dispatchEvent(e);			
		}
		protected function onClick():void{
		}
		protected function addEventListenerOn(elem:Sprite):void{
			if(listenerList.indexOf(elem) == -1){
				listenerList.push(elem);
			}
			if(enableListening){
				elem.buttonMode = true;
				elem.addEventListener(MouseEvent.MOUSE_OVER , mouseOver);
				elem.addEventListener(MouseEvent.CLICK , mouseClick);
				elem.addEventListener(MouseEvent.MOUSE_DOWN , mouseDown);
				elem.addEventListener(MouseEvent.MOUSE_OUT , mouseOut);
				elem.addEventListener(MouseEvent.MOUSE_UP , mouseUp);
			}
		}
		protected function removeEventlistenerOn(elem:Sprite):void{
			elem.buttonMode = false;
			elem.removeEventListener(MouseEvent.MOUSE_OVER , mouseOver);
			elem.removeEventListener(MouseEvent.CLICK , mouseClick);
			elem.removeEventListener(MouseEvent.MOUSE_DOWN , mouseDown);
			elem.removeEventListener(MouseEvent.MOUSE_OUT , mouseOut);
			elem.removeEventListener(MouseEvent.MOUSE_UP , mouseUp);
		}
		protected function showFocus():void{
			for each(var mo:MovieClip in focusClips){
				mo.gotoAndStop(FRAME_FOCUS);
			}
		}		
		protected function showBlur():void{
			for each(var mo:MovieClip in blurClips){
				mo.gotoAndStop(FRAME_BLUR);
			}
		}
		protected function showSelectedFocus():void{
			for each(var mo:MovieClip in selectedFocusClips){
				mo.gotoAndStop(FRAME_SELECTED_FOCUS);
			}
		}
		public function lock():void{
			for each(var elem in listenerList){
				removeEventlistenerOn(elem);
			}
			
			enableListening = false;
			initView();
		}
		public function unlock():void{
			enableListening = true;
			for each(var elem in listenerList){
				addEventListenerOn(elem);
			};
			initView();
		}
		protected function showSelectedBlur():void{
			for each(var mo:MovieClip in selectedBlurClips){
				mo.gotoAndStop(FRAME_SELECTED_BLUR);
			}
		}
		protected function showLocked():void{
			for each(var mo:MovieClip in lockedClips){
				mo.gotoAndStop(FRAME_LOCKED);
			}
		}
	}
}